package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理相关接口")
public class SetMealController {

    @Autowired
    SetMealService setMealService;


    /**
     * 根据类型查询分类
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "增加套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO)
    {
        setMealService.saveWithSetMealDish(setmealDTO);
        return Result.success();
    }
    /**
     * 分页查询
     * @param
     * @return
     */
     @GetMapping("page")
     @ApiOperation(value = "分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        PageResult pageResult=setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @DeleteMapping
    @ApiOperation(value ="批量删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids)
    {
         setMealService.deleteBatch(ids);
         return Result.success();
    }


    /**
     * 根据id查询套餐和相关菜品(回显)
     * @param
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐和相关菜品(回显)")
    public Result<SetmealVO> getById(@PathVariable Long id)
    {
        SetmealVO setmealVO= setMealService.getByIdWithSetmeal(id);
         return  Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param
     * @return
     */
    @PutMapping
    @ApiOperation(value ="修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO)
    {
         setMealService.updateWithSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 停售和启售
     * @param
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "停售和启售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id)
    {
        setMealService.status(status,id);
        return Result.success();
    }

}
